
//JS
layui.use(['layer','table','upload'], function() {
    var table = layui.table,
        upload = layui.upload,
        layer = layui.layer,
        $ = layui.$;
    layui.layer.load(0, {shade: [0.5, '#fff']});

    //绑定批量导入按钮(教师)
    $('#titleTeacher-tadds-btn').click(function () {
        layer.open({
            type: 1
            , offset: 0
            , content: $("#titleTeacher-tadds-box")
            , end: function () {
                $('#titleTeacher-tadds-box').css('display', 'none');
            }
        });
    });
    //文件批量上传--教师
    upload.render({
        elem: '#titleTeacher-tfile-choose'
        ,url: '' //此处配置你自己的上传接口即可
        ,auto: false
        //,multiple: true
        ,bindAction: '#titleTeacher-tfile-upload'
        ,done: function(res){
            layer.msg('上传成功');
            console.log(res)
        }
    });

    <!-- 动态表格 -->
    table.render({
        elem: '#titleTeacher-table',
        toolbar: '#titleTeacherToolBar',
        url: '/getTitle',
        where: {type: 2, queryKey: ''},
        page: true,
        height:500,
        cols:[
            [ //标题栏
                {field: 'xtId', title: '编号', width: 90, align: 'center', sort: true,fixed:"left"},
                {field: 'title', title: '课题名称', align: 'center'},
                {field: 'no', title: '申请教师编号', width: 130, align: 'center', hide: true},
                {field: 'addtime', title: '申请时间', width: 130, align: 'center', hide: true},
                {field: 'from', title: '课题来源', width: 120, align: 'center', hide: true},
                {field: 'categoty', title: '课题类别', width: 100, align: 'center', hide: true},
                {field: 'nature', title: '课题性质', width: 140, align: 'center', hide: true},
                {field: 'intr', title: '课题简介', align: 'center',width:200, hide: true},
                {field: 'collegeName', title: '学院', align: 'center'},
                {field: 'majorName', title: '专业', align: 'center'},
                {field: 'tno', title: '指导教师工号', width: 140, align: 'center'},
                {field: 'tName', title: '指导教师姓名', width: 140, align: 'center'},
                {
                    field: 'state', title: '审核状态', width: 140, align: 'center', fixed: 'right', templet: function (res) {
                        if (res.state === '审核完成') {
                            return '<span style="color: green">审核完成</span>';
                        } else {
                            return '<span style="color: #1E9FFF">审核中</span>';
                        }
                    }
                },
                {
                    field: 'isPass', title: '是否通过', width: 140, align: 'center', fixed: 'right', templet: function (res) {
                        if (res.isPass === '不通过') {
                            return '<span style="color: red">不通过</span>';
                        } else if (res.isPass === '通过') {
                            return '<span style="color: green">通过</span>';
                        } else {
                            return '<span style="color: #1E9FFF">审核中</span>';
                        }
                    }
                },
                {field: 'opinoin', title: '审核意见', align: 'center', width: 120, hide: true},
                {title: '操作', width: 140, toolbar: '#titleTeacherBtn', align: 'center',fixed:"right"}
            ]
        ]
        , even: true
        , limits: [14, 28, 42]
        , limit: 14
        , text: {
            none: '<div class="layui-anim layui-anim-scaleSpring"  style="margin-top: 150px"><i class="layui-icon" style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><br><br><br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有没找到数据呢</div></div>'
            ,
            error: '<div style="margin-top: 150px"><i class="layui-icon"  style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有出错了呢</div></div>'
        }
    });
    //监听行工具事件
    table.on('tool(titleTeacher-table)', function (obj) {
        let data = obj.data;
        if (obj.event === 'titleT-check') {
            if (data.state === '审核完成') {
                //判断是否审核完毕
                layer.msg('该选题已经审核完毕', {offset: '250px', time: 1500, anim: 6});
                return false;
            }
            layer.open({
                type: 1
                , offset: 10
                , skin: 'layui-layer-rim'
                , area: ['740px', '750px']
                , content: $('#check-task-teacher')
                , title: '课题详情内容'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    /*设置回显数据*/
                    setTaskDetail(data);
                    $("#task-check-teacher-tbody").html($("#task-detail-tbody").html());
                }
                , end: function () {
                    $('#check-task-teacher').css('display', 'none');
                }
            });
        } else if (obj.event === 'titleT-query') {
            layer.open({
                type: 1
                , offset: 10
                , skin: 'layui-layer-rim'
                , area: ['740px', '750px']
                , content: $('#titleTeacher-show-box')
                , title: '课题详情内容'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    setTaskDetail(data);
                }
                , end: function () {
                    $('#titleTeacher-show-box').css('display', 'none');
                }
            });
        }
    });
    layui.layer.closeAll('loading');

    //通过审核
    $("#check-pass").click(function () {
        let result = '通过';
        checkResult(result);
    });

    //不通过审核
    $("#check-no-pass").click(function () {
        let result = '不通过';
        checkResult(result);
    });

});
//设置课题回显数据
function setTaskDetail(data) {
    $("#task-id").html(data.xtId);
    $("#task-title").html(data.title);
    $("#task-tno").html(data.no);
    $("#task-addtime").html(data.addtime);
    $("#task-from").html(data.from);
    $("#task-category").html(data.categoty);
    $("#task-nature").html(data.nature);
    $("#task-intr").html(data.intr);
    $("#task-college").html(data.collegeName);
    $("#task-major").html(data.majorName);
    $("#task-state").html(data.state);
    $("#task-teacherName").html(data.tName);
    $("#task-teacherNO").html(data.tno);
    $("#task-opinion").html(data.opinoin);
}

//提交获取审核结果
function checkResult(result) {
    layui.layer.closeAll('page');
    let opinion = $("#check-opinion").val().trim();
    if (opinion === null || opinion === ''){
        layui.layer.msg('请填写审核意见', {offset: '250px', time: 1500, anim: 6});
        return false;
    }
    let id = $("#task-id").html().trim();
    let loadPage = layui.layer.load(0, {shade: [0.5, '#fff']});
    $.ajax({
        url: "/checkTitle",
        type: 'post',
        data: {
            result: result, opinion: opinion,id:id
        },
        dataType: 'json',
        success: function (data) {  //调用接口成功
            if (data === true || data === 'true') {
                layer.close(loadPage);
                layer.closeAll('page');
                layui.layer.msg('审核成功！', {offset: '300px', time: 2500, anim: 0});
                layui.table.reload('titleTeacher-table', {
                    where: {queryKey: '', type: 2}
                });
            } else {
                layui.layer.close(loadPage);
                layui.layer.alert('审核失败！');
            }
        },
        error: function () {  //接口错误
            layui.layer.close(loadPage);
            layui.layer.alert('服务器出错啦,请稍后再试！');
        }
    });
}